package com.o2opuzi.pub.controller

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.o2opuzi.lib.db.Terms
import com.o2opuzi.lib.util.FieldErrorUtils;
import com.o2opuzi.pub.dao.MenuDao;
import com.o2opuzi.pub.dao.RoleDao;
import com.o2opuzi.pub.dao.UserDao;
import com.o2opuzi.pub.model.Role;
import com.o2opuzi.pub.model.User;

@RestController
@RequestMapping("/admin/role")
class RoleController {
	
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private MenuDao menuDao;
	
	
	/**
	 * 新增角色界面所需的数据
	 * @auther Y.hj
	 * @return
	 */
	@GetMapping("/create")
	def create(){
		return [
			data:[
				menu:menuDao.getMenuTreeList()
			]
		];
	}
	
	
	/**
	 * 新增角色
	 * @auther Y.hj
	 * @param role
	 * @param result
	 * @return
	 */
	@PostMapping("/create")
	def create(@RequestBody @Valid Role role,BindingResult result){
		if(result.hasErrors()){
			return [
				code:0,
				messages:FieldErrorUtils.getErrors(result)
			];
		}
		if(roleDao.exists("name", role.name)){
			return [
				code:0,
				messages:[text:"角色名字已存在"]
			];
		}
		
		if(roleDao.save(role) > 0){
			return [
				code:1,
				messages:[text:"新增成功"]
			];
		}
		return [
			code:0,
			messages:[text:"新增失败"]
		];
		
	}
	
	/**
	 * 删除角色
	 * @auther Y.hj
	 * @param role
	 * @return
	 */
	@PostMapping("/delete")
	@Transactional
	def delete(@RequestBody Role role){
		if(role.id == null || role.id == 1){
			return [
				code:0,
				messages:[text:"角色不存在"]
			];
		}
		//删除角色
		if(roleDao.deleteById(role.id) > 0){
			//更新账号角色信息
			userDao.updateRoleNull(role.id); 
			return [
				code:1,
				messages:[text:"删除成功"]
			];
		}
		return [
			code:0,
			messages:[text:"角色不存在或已删除"]
		];
	}
	
	/**
	 * 修改
	 * @auther Y.hj
	 * @param role
	 * @param result
	 * @return
	 */
	@PostMapping("/update")
	def update(@RequestBody @Valid Role role,BindingResult result){
		if(result.hasErrors()){
			return [
				code:0,
				messages:FieldErrorUtils.getErrors(result)
			];
		}
		if(role.id == null){
			return [
				code:0,
				messages:[text:"角色id不能为空"]
			];
		}
		if(roleDao.find(new Terms(["name","=",role.name],["id","<>",role.id])) != null){
			return [
				code:0,
				messages:[text:"角色名字已存在"]
			];
		}
		
		if(roleDao.updateById(role) > 0){
			return [
				code:1,
				messages:[text:"修改成功"]
			];
		}
		return [
			code:0,
			messages:[text:"修改失败"]
		];
		
	}
	
	@GetMapping("/update")
	def update(Integer id){
		return [
			code:1,
			data:[
				role:roleDao.findById(id),
				menu:menuDao.getMenuTreeList()
			]
		];
	}
	
	@GetMapping("/get/all")
	def getRoleAll(){
		return [
			code:1,
			data:roleDao.findList()
		];
	}
	
}
